package com.bwie.service.impl;

import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.pojo.TbMenu;
import com.bwie.service.TbMenuService;
import com.bwie.mapper.TbMenuMapper;
import com.bwie.utils.R;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
* @author 叶雨洁
* @description 针对表【tb_menu(权限表)】的数据库操作Service实现
* @createDate 2025-07-18 19:25:59
*/
@Service
public class TbMenuServiceImpl extends ServiceImpl<TbMenuMapper, TbMenu>
    implements TbMenuService{

    @Autowired
    TbMenuMapper menuMapper;

        @Override
        public R getMenuList(HttpServletRequest request) {

            String token = request.getHeader("token");

            if(token==null){
                return R.ERROR(500,"token为空");
            }

            Claims claims = Jwts.parser().setSigningKey("bwie").parseClaimsJws(token).getBody();
            Integer userId = claims.get("userId", Integer.class);

            List<TbMenu> menuList = menuMapper.selectByUserId(userId);

            List<Tree<String>> build = TreeUtil.build(menuList, "0", (menu, tree) -> {
                tree.setName(menu.getMenuName());
                tree.setParentId(menu.getMenuPid() + "");
                tree.setId(menu.getMenuId() + "");
                tree.putExtra("menuPath", menu.getMenuPath());
                tree.putExtra("createBy", menu.getCreateBy());
            });

            return R.OK(build);
        }


}




